package com.leetcode;

import com.leetcode.common.ListNode;

/**
 * 21. 合并两个有序链表
 */
public class Solution21 {

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode dummyHead = new ListNode();
        ListNode cur1 = list1;
        ListNode cur2 = list2;
        ListNode cur = dummyHead;
        while (cur1 != null || cur2 != null) {
            if (cur1 == null ) {
                cur.next = cur2;
                cur2 = cur2.next;
            } else if (cur2 == null || cur1.val <= cur2.val) {
                cur.next = cur1;
                cur1 = cur1.next;
            } else {
                cur.next = cur2;
                cur2 = cur2.next;
            }
            cur = cur.next;
        }
        return dummyHead.next;
    }

}
